package org.example.studentmanager.controller;

import org.example.studentmanager.common.utils.R;
import org.example.studentmanager.entity.CourseSelectionsStudent;
import org.example.studentmanager.entity.Score;
import org.example.studentmanager.entity.Student;
import java.util.List;
import org.example.studentmanager.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("/courses")
    public R<Integer> batchSelectCourses(@RequestBody List<CourseSelectionsStudent> selections) {
        return R.success(studentService.batchSelectCourses(selections));
    }

    /**
     * 根据学生的id查询学生的所有成绩信息
     * @param id 学生id
     * @return 学生成绩的所有信息
     */
    @GetMapping("/scores/{user_id}")
    public R<List<Score>> getScores(@PathVariable("user_id") Integer id) {
        List<Score> scoreList = studentService.getScores(id);
        if (scoreList == null) {
            return R.error(0, "当前学生id不存在");
        } else if (scoreList.isEmpty()) {
            // 应该会有这种情况吧
            return R.error(0, "教师还未上传成绩");
        }
        return R.success(scoreList);
    }

    @GetMapping("/courses/{studentId}")
    @ResponseBody
    public R<List<CourseSelectionsStudent>> getCourseSelectionsByStudentId(@PathVariable("studentId") Integer studentId) {
        return R.success(studentService.getCourseSelectionsByStudentId(studentId));
    }

    @GetMapping("/{user_id}")
    public R<Student> getStudentById(@PathVariable("user_id") Integer user_id) {
        return R.success(studentService.getStudentById(user_id));
    }

    /**
     * 根据学生id和课程删除选课记录
     * @param studentId 学生id
     * @param courseId 课程id
     * @return 删除是否成功，成功返回 true，失败返回错误信息
     */
    @GetMapping("/courses/delete")
    public R<Boolean> dropCourse(@RequestParam("student_id") Integer studentId, @RequestParam("course_id") Integer courseId) {
        Boolean result = studentService.dropCourse(studentId, courseId);
        if (result) {
            return R.success(true);
        } else {
            return R.error(0, "删除失败");
        }
    }

    @GetMapping("/selections/{studentId}")
    public R<List<CourseSelectionsStudent>> getCourseSelectionsWithDetails(@PathVariable Integer studentId) {
        return R.success(studentService.getCourseSelectionsWithDetails(studentId));
    }

    /**
     * 全新接口：根据学生 ID 查询选课信息
     * @param studentId 学生 ID
     * @return 选课信息列表
     */
    @GetMapping("/courses/chose/{studentId}")
    public R<List<CourseSelectionsStudent>> getNewCourseSelectionsByStudentId(@PathVariable("studentId") Integer studentId) {
        return R.success(studentService.getNewCourseSelectionsByStudentId(studentId));
    }
}





